
package pl.genapp.stocksense.libraries;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;

public class SentGraphPie extends SentGraph {
	
	private static final int ANGLE_EXPAND = 60;
	private static final int BACKGROUND_ALPHA = 0x20;
	private static final int PIE_ALPHA = 0x99;
	private static final int PIE_SELECTED_ALPHA = 0xFF;
	private Paint[] pieFill;
	private Paint[] pieBackground;
	private Paint circle;
	private int selectedSentimentNo = -1;
	
	public SentGraphPie(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		setupPaints();
		
		super.setLegendRendered(legendRendered);
	}
	
	/**
	 * Ustawia wartosci obiektow Paint
	 */
	private void setupPaints() {
		
		pieFill = new Paint[NUMBER_OF_SENTIMENTS];
		pieBackground = new Paint[NUMBER_OF_SENTIMENTS];
		
		for (int i = 0; i < NUMBER_OF_SENTIMENTS; i++) {
			
			// pieFill
			pieFill[i] = new Paint();
			pieFill[i].setAntiAlias(true);
			pieFill[i].setStyle(Paint.Style.FILL);
			pieFill[i].setColor(getSentColorWithAlpha(i, PIE_ALPHA));
			
			// pieBackground
			pieBackground[i] = new Paint();
			pieBackground[i].setAntiAlias(true);
			pieBackground[i].setStyle(Paint.Style.FILL);
			pieBackground[i].setColor(getSentColorWithAlpha(i, BACKGROUND_ALPHA));
		}
		
		circle = new Paint();
		circle.setAntiAlias(true);
		circle.setStyle(Paint.Style.STROKE);
		circle.setColor(0xFFCBD1CE);
		circle.setStrokeWidth(1);
		
	}
	
	private RectF getProperSizeRectangle(float part) {
		
		float pieSize = part * getGraphSize() / 2;
		
		return new RectF(getGraphCenter() - pieSize, getGraphCenter() - pieSize, getGraphCenter() + pieSize, getGraphCenter() + pieSize);
		
	}
	
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
			
		// rysujemy kolko kolko w tle
		canvas.drawCircle(getGraphCenter(), getGraphCenter(), getGraphSize() / 2, circle);
		
		// rysujemy pie i bg pie
		for (int i = 0; i < NUMBER_OF_SENTIMENTS; i++) {
			
			canvas.drawArc(getProperSizeRectangle(1.0f), i * ANGLE_EXPAND, ANGLE_EXPAND, true, pieBackground[i]);
			
			canvas.drawArc(getProperSizeRectangle(sentValues[i]), i * ANGLE_EXPAND, ANGLE_EXPAND, true, pieFill[i]);
			
		}
		
		if (getLegendRendered() != null) {
			getLegendRendered().drawLegend(canvas);
			
		}
		
	}
	
	@Override
	void selectSentiment(int sentNo) {
		
		if (selectedSentimentNo != -1) pieFill[selectedSentimentNo].setColor(getSentColorWithAlpha(selectedSentimentNo, PIE_ALPHA));
		
		pieFill[sentNo].setColor(getSentColorWithAlpha(sentNo, PIE_SELECTED_ALPHA));
		selectedSentimentNo = sentNo;
		
		onSentClickListener.onSentClick(sentNo);
		
		postInvalidate();
		
	}
	
	@Override
	int getClickedSentNumber(float x, float y) {
		
		float delta_x = x - getGraphCenter();
		float delta_y = y - getGraphCenter();
		
		double theta_radians = Math.atan2(delta_y, delta_x);
		
		int angle = (int) (theta_radians * (180 / Math.PI));
		
		if (angle < 0) angle = 360 + angle;
		
		int id = (int) angle / 60;
		
		Log.d("stocksense", id + " ");
		
		return id;
		
	}
	
	private SentGraph.LegendRenderer legendRendered = new LegendRenderer(new int[] { 0, 0, 0, 0 }, new int[] { 30, 30 }, new int[] { 0, 10 }, this) {
		
		void drawIcon(Canvas canvas, RectF frame, int sentNo) {
			canvas.drawArc(frame, -30, 60, true, pieFill[sentNo]);
		};
		
	};
}
